﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Collections.Generic;

namespace HZ.DrugTrade.Web.Enterprise.OrderManage
{
    public partial class OrderUndistributeDetailCheck : BasePage
    {
        public OrderUndistributeDetailCheck()
        {
            this.ModuleNo = Module.配送单管理.ToString();
        }

        IBLL_Purchase iPurchase = Factory.BLL_Purchase();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据 

                txtGoodsName.Text = "";
                txtOutLookc.Text = "";
                txtStartDate.Text = "";
                txtEndDate.Text = "";
                txtHospitalName.Text = "";
                txtpinyincode.Text = "";
                txtMaterial.Text = "";

                //txtBatchRecordID.Text = "";
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        public void Bind()
        {
            HZ.DrugTrade.Model.Query.PurchaseOrderDetail QueryDistribution = new HZ.DrugTrade.Model.Query.PurchaseOrderDetail();

            QueryDistribution.StartTimeFrom = new HZ.Common.ConvertHelper(txtStartDate.Text.Trim()).ToDateTime;
            QueryDistribution.StartTimeTo = new HZ.Common.ConvertHelper(txtEndDate.Text.Trim()).ToDateTime;
            QueryDistribution.OrderDetailState = 3;
            //QueryDistribution.BatchRecordID = txtBatchRecordID.Text.Trim();
            QueryDistribution.HospitalName = this.txtHospitalName.Text.Trim();
            //QueryDistribution.PinyinCode = this.txtPinyinCode.Text.Trim();
            QueryDistribution.GoodsName = this.txtGoodsName.Text.Trim();
            QueryDistribution.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            QueryDistribution.Material = this.txtMaterial.Text.Trim();
            QueryDistribution.Outlookc = this.txtOutLookc.Text.Trim();
            QueryDistribution.CompanyID_PS = CurrentAccount.GetUser().Org.OrgID;
            QueryDistribution.ProjectID = this.ddlProjectid.SelectedValue;

            QueryDistribution.SortFields = "ExecuteTime desc ,OrderName ASC,HospitalName ASC,GoodsName ASC ";
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalrecord = 0;
            try
            {
                IList<HZ.DrugTrade.Model.View.PurchaseOrderDetail> vDistributionList = iPurchase.GetPurchaseOrderDetailByWhere(QueryDistribution, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
                gvwProduceCatalog.AutoGenerateColumns = false;
                gvwProduceCatalog.DataSource = vDistributionList;
                gvwProduceCatalog.DataBind();
                AspNetPager1.RecordCount = totalrecord;
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }

            if (gvwProduceCatalog.Rows.Count <= 0)
            {
                btnExport.Visible = false;
            }
        }

        private void PageError(string p, string p_2)
        {
            throw new NotImplementedException();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            HZ.DrugTrade.Model.Query.PurchaseOrderDetail QueryDistribution = new HZ.DrugTrade.Model.Query.PurchaseOrderDetail();

            QueryDistribution.StartTimeFrom = new HZ.Common.ConvertHelper(txtStartDate.Text.Trim()).ToDateTime;
            QueryDistribution.StartTimeTo = new HZ.Common.ConvertHelper(txtEndDate.Text.Trim()).ToDateTime;
            QueryDistribution.OrderDetailState = 3;
            QueryDistribution.HospitalName = this.txtHospitalName.Text.Trim();
            //QueryDistribution.PinyinCode = this.txtPinyinCode.Text.Trim();
            QueryDistribution.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            QueryDistribution.GoodsName = this.txtGoodsName.Text.Trim();
            QueryDistribution.Material = this.txtMaterial.Text.Trim();
            QueryDistribution.Outlookc = this.txtOutLookc.Text.Trim();
            QueryDistribution.CompanyID_PS = CurrentAccount.GetUser().Org.OrgID;
            QueryDistribution.ProjectID = this.ddlProjectid.SelectedValue;
            QueryDistribution.SortFields = "ExecuteTime desc ,OrderName ASC,HospitalName ASC,GoodsName ASC ";

            int totalrecord = 0;
            IList<HZ.DrugTrade.Model.View.PurchaseOrderDetail> vDistributionList = iPurchase.GetPurchaseOrderDetailByWhere(QueryDistribution, 1, 0, out totalrecord);

            DataTable dt = HZ.Data.ModelConvert.GetDataTableFromModel(vDistributionList);

            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwProduceCatalog);
        }

        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (ddlProjectid.SelectedItem.Value == "0002")
                {
                    e.Row.Cells[2].Text = "自主报价";
                }
                else
                {
                    e.Row.Cells[2].Text = "采购限价";
                }
            }
        }
    }
}
